<?php
namespace app\yiluqianxing\home;

use think\Controller;
use think\Db;
use think\Request;

class Xiaochengxu extends Controller{

    //获取个人打卡记录
    function index($openid=0){
        $data=Db::table('dp_ylqx_uploads')
            ->where('openid',$openid)
            ->order('id desc')
            ->limit(15)
            ->select();
        foreach ($data as $key=>$v){
            $data[$key]['time']=date('Y-m-d',$v['time']);
        }
        return json_encode($data);
    }






    //获取用户点数
    function getuserpoint($openid=''){
        $data=Db::table('dp_ylqx_user')
            ->where('openid',$openid)
            ->find();
        $data['point']=Db::table('dp_ylqx_uploads')
            ->where(['status'=>1,'openid'=>$openid])
            ->sum('status');
        echo json_encode($data);
    }

    /**
     * 获取商品详情
     * @param int $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getgoodinf($id=1){
        $data=Db::table('dp_ylqx_goods')
            ->where('id',$id)
            ->find();
        $find=Db::table('dp_admin_attachment')
            ->where('id',$data['img'])
            ->find();
        $data['img2']=$find['path'];
        $data['content'] = str_replace('/uploads/',"https://www.lightrain.org.cn/uploads/",$data['content']);
        echo json_encode($data);

    }

    /**
     * 获取商品列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function goodslist(){
        $data=Db::table('dp_ylqx_goods')
            ->order('id desc')
            ->select();
        foreach ($data as $key=>$v){
            $find=Db::table('dp_admin_attachment')
                ->where('id',$v['img'])
                ->find();
            $data[$key]['img2']=$find['path'];
        }
        echo json_encode($data);
    }

    //获取打卡排行数据
    function paihang(){
        $data=Db::table('dp_ylqx_uploads')
            ->field('openid')
            ->distinct(true)
            ->select();
        foreach ($data as $key =>$v){
            $data[$key]['count']=Db::table('dp_ylqx_uploads')
                ->where('openid',$v['openid'])
                ->count();
            $find=Db::table('dp_ylqx_user')
                ->where('openid',$v['openid'])
                ->find();
            if(!$find){
                unset($data[$key]);
            }else{
                $data[$key]['userinf']=$find;
            }
        }
        $last_names = array_column($data,'count');
        array_multisort($last_names,SORT_DESC,$data);
//        dump($data);
        echo json_encode($data);
    }

    //获取个人openid
    function getopenid($code=0){
        $code=$code;
        $appid='wxa7a3bded93af6256';
        $secret = "414cbd4b7ea75785a0a266b8530a2fdd";
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);

        return $res;

    }

    //个人用户登陆
    function saveuser(){
        header("Content-type:text/html;charset=utf8");

        $data['nickname']=$_GET["nickname"];
        $data['avatarurl']=$_GET["avatarUrl"];
        $data['openid']=$_GET["openid"];
        $data['time']=time();
        $find=Db::table('dp_ylqx_user')
            ->where('openid',$data['openid'])
            ->find();
        if(!$find){
            $save=Db::table('dp_ylqx_user')
                ->data($data)
                ->insert();
        }
        return json_encode($data);
    }

    //根据openid上传图片并留下路径
    function upload($openid=0){
        if($openid===0){
            return 1;
        }
        //每人每天只可以上传一张照片
        $today=strtotime(date('Y-m-d 00:00:00'));

        $find=Db::table('dp_ylqx_uploads')
            ->where('openid',$openid)
            ->where('time','egt',$today)
            ->order('id desc')
            ->find();
        if($find){
            return 1;
        }

        $data['openid']=$openid;
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( './uploads');
        if($info){
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $data['path']=$info->getSaveName();
            $data['time']=time();
            Db::table('dp_ylqx_uploads')
                ->data($data)
                ->insert();
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }

}